﻿
//抢购状态
function ShowPageState(state) {
    var div;
    window.currentstate = state;
    if (state === 0) {
        if (window.isroommark || window.iscarmark) {
            window.layer.msg("当前项目未开始！即将跳转到公告页面！", function () {
                loginout();
            });
        }
        else if (window.ismonitor) {
            window.timeout();//去掉
        }
    } else if (state === 1) {
        if (window.isroommark || window.iscarmark) {
            window.layer.msg("当前项目未开始！即将跳转到公告页面！",
                function () {
                    loginout();
                });
        }
        else if (window.ismonitor) {
            div = GetDownCountDivMon(state);
            $("body").append(div);
            //项目开始倒计时
            CountDownFun($('#countdown'), state);
        }
    } else if (state === 2) {
        //未开始
        //显示预览倒计时
        if (window.isroommark || window.iscarmark) {
            div = GetDownCountDiv(state);
            $("body").append(div);
            //项目开始倒计时
            CountDownFun($('#countdown'), state);
        }
        else if (window.ismonitor) {
            div = GetDownCountDivMon(state);
            $("body").append(div);
            //项目开始倒计时
            CountDownFun($('#countdown'), state);
        }
    } else if (state === 3) {
        //抢购倒计时
        if (window.isroommark || window.iscarmark) {
            div = GetDownCountDiv(state);
            $("body").append(div);
            CountDownFun($('#auctioncd'), state);
        }
        else if (window.ismonitor) {
            div = GetDownCountDivMon(state);
            $("body").append(div);
            //项目开始倒计时
            CountDownFun($('#countdown'), state);
        }
    } else if (state === 4) {
        //进行中
        window.timeout();//去掉
        $(".cover_bg").hide();
        //显示距离结束倒计时
        if (window.isroommark || window.iscarmark) {
            div = GetDownCountDiv(state);
            $("body").append(div);
            //项目结束倒计时
            CountDownFun($('#countdown'), state);
        }
        else if (window.ismonitor) {
            div = GetDownCountDivMon(state);
            $("body").append(div);
            //项目开始倒计时
            CountDownFun($('#countdown'), state);
        }
    } else if (state === 5) {
        if (window.isroommark || window.iscarmark) {
            window.ShowPublicity();
            div = GetDownCountDiv(state);
            $("body").append(div);
            //            window.timeend();
        }
        else if (window.ismonitor) {
            window.timeout();//去掉
        }

        //已结束
        //显示用户结果
    } else if (state === 6) {
        if (window.isroommark || window.iscarmark) {
            window.layer.msg("当前项目已经结束！即将跳转到公告页面！",
                function () {
                    loginout();
                });
        }
        else if (window.ismonitor) {
            window.timeout();//去掉
        }
    }
}

//倒计时div
function GetDownCountDiv(type) {
    $("#outdiv").remove();//去掉长时间倒计时与短时间倒计时
    $("#auctioncd").remove();
    $("#countdown").remove();
    var div = $("<div style='text-align:center;' id='outdiv'></div>");
    var height = 80;
    var width = 400;
    var left = (window.innerWidth - width) / 2;
    if (type === 2 || type === 4 || type === 5) {
        var text = "";
        var top = 0;
        if (type === 2) {
            text = "离预览结束还有";
        }
        if (type === 4) {
            text = "离抢购结束还有";
        }
        if (type === 5) {
            text = "本次抢购已经结束！";
        }
        div.css({
            left: left,
            top: top,
            position: "absolute",
            width: "280px",
            "z-index": "10"
        });
        div.append("<div class='topcountdown'>" + text + "</div>");
        div.append("<div id='countdown' style='color: #ffe800;'></div>");
    } else if (type === 3) {
        $(".cover_bg").show();
        div.append("<div class='mincountdown' style=''>抢购开始倒计时！</div>");
        div.css({
            height: height,
            width: width,
            left: left,
            top: "240px",
            position: "absolute",
            "z-index": "9999"
        });
        div.append("<div id='auctioncd'></div>");
    }
    return div;
}
//管理界面倒计时div
function GetDownCountDivMon(type) {
    $("#outdiv").remove();//去掉长时间倒计时与短时间倒计时
    $("#auctioncd").remove();
    $("#countdown").remove();
    var div = $("<div style='text-align:center;' id='outdiv'></div>");
    if (type >= 1 && type <= 5) {
        var text = "";
        if (type === 1) {
            text = "离预览开始还有";
        }
        if (type === 2) {
            text = "离预览结束还有";
        }
        if (type === 3) {
            text = "离开始抢购还有";
        }
        if (type === 4) {
            text = "离抢购结束还有";
        }
        if (type === 5) {
            text = "抢购已经结束！";
        }
        div.css({
            right: 0,
            bottom: 0,
            position: "absolute",
            width: "260px",
            "z-index": "10"
        });
        div.append("<div class='topcountdown' style='width:100%; text-align:center; font-size: 20px; color: #000; font-weight: bold;'>" + text + "</div>");
        div.append("<div id='countdown' style='line-height: 40px; color: #000;'></div>");
    }
    return div;
}

//倒计时函数
function CountDownFun(obj, state) {
    window.getCountDown({
        obj: obj,
        timestamp: timearr[state],
        format: '%H小时%M分%S秒',
        clear: true
    });
}


//保持登录
function KeepLogin() {
    if (window.isroommark || window.iscarmark) {
        //登陆
        var sjson = {
            action: "redis",
            msg: {
                Action: "join", Operation: window.user
            }
        }
        websocket.send(JSON.stringify(sjson));
    }
}